package cn.kgc.service;

import cn.kgc.utils.EmptyUtils;
import cn.kgc.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.kgc.mapper.DmUserMapper;
import cn.kgc.pojo.DmUser;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
* Created by 北大课工场
*/
@RestController
public class RestDmUserService {

@Autowired
private DmUserMapper dmUserMapper;

@RequestMapping(value = "/getDmUserById",method = RequestMethod.POST)
public DmUser getDmUserById(@RequestParam("id") Long id)throws Exception{
return dmUserMapper.getDmUserById(id);
}

@RequestMapping(value = "/getDmUserListByMap",method = RequestMethod.POST)
public List<DmUser>  getDmUserListByMap(@RequestParam Map<String,Object> param)throws Exception{
return dmUserMapper.getDmUserListByMap(param);
}

@RequestMapping(value = "/getDmUserCountByMap",method = RequestMethod.POST)
public Integer getDmUserCountByMap(@RequestParam Map
<String,Object> param)throws Exception{
return dmUserMapper.getDmUserCountByMap(param);
}

@RequestMapping(value = "/qdtxAddDmUser",method = RequestMethod.POST)
public Integer qdtxAddDmUser(@RequestBody DmUser dmUser)throws Exception{
dmUser.setCreatedTime(new Date());
dmUserMapper.insertDmUser(dmUser);
return dmUser.getId().intValue();
}

@RequestMapping(value = "/qdtxModifyDmUser",method = RequestMethod.POST)
public Integer qdtxModifyDmUser(@RequestBody DmUser dmUser)throws Exception{
dmUser.setUpdatedTime(new Date());
return dmUserMapper.updateDmUser(dmUser);
}

@RequestMapping(value = "/qddelDmUser",method = RequestMethod.GET)
public Integer qdtxDelDmUser(@RequestParam("id") Long id)throws Exception{

return dmUserMapper.deleteDmUserById(id);
}

  @RequestMapping(value = "/generateToken", method = RequestMethod.POST)
 public String generateToken(@RequestBody DmUser dmUser) {
        //保证令牌唯一（保证一定是唯一的）
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("token-");
        stringBuffer.append("PC-");
        String md5 = MD5.getMd5(EmptyUtils.isEmpty(dmUser.getPhone())?dmUser.getWxUserId():dmUser.getPhone(), 8);
        stringBuffer.append(md5 + "-");
        stringBuffer.append(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + "-");
        //6位随机数
        String substring = UUID.randomUUID().toString().substring(0, 6);
        stringBuffer.append(substring);
        return stringBuffer.toString();


    }

    @RequestMapping(value = "/checkLoginByPassword", method = RequestMethod.POST)
    public DmUser checkLoginByPassword(@RequestBody DmUser dmUser) throws Exception {
        return dmUserMapper.checkLoginByPassword(dmUser.getPhone(), dmUser.getPassword());
    }

}
